﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Text.RegularExpressions;
using SYcwpb.Commands;
using SYcwpb.Helpers;
using SYcwpb.Access;
using System.Data;
using System.ComponentModel;

namespace SYcwpb.ViewModels
{
    class StatisticsSettingViewModel : ViewModelBase, IDataErrorInfo
    {
        /// <summary>
        /// 参数字典
        /// </summary>
        private Dictionary<string, string> DicParaMeters = new Dictionary<string, string>();

        private StatisticsAccess _statisticsAccess = new StatisticsAccess();

        #region 参数设置
        private string _inMiles;
        public string InMiles
        {
            get { return _inMiles; }
            set
            {
                if (_inMiles == value)
                    return;
                _inMiles = value;
                OnPropertyChanged("InMiles");
            }
        }

        private string _outMiles;
        public string OutMiles
        {
            get { return _outMiles; }
            set
            {
                if (_outMiles == value)
                    return;
                _outMiles = value;
                OnPropertyChanged("OutMiles");
            }
        }

        private string _upToChangedMiles;
        public string UpToChangedMiles
        {
            get { return _upToChangedMiles; }
            set
            {
                if (_upToChangedMiles == value)
                    return;
                _upToChangedMiles = value;
                OnPropertyChanged("UpToChangedMiles");
            }
        }

        private string _hkhupToChangedMiles;
        public string HKHUpToChangedMiles
        {
            get { return _hkhupToChangedMiles; }
            set
            {
                if (_hkhupToChangedMiles == value)
                    return;
                _hkhupToChangedMiles = value;
                OnPropertyChanged("HKHUpToChangedMiles");
            }
        }

        private string _downToChangedMiles;
        public string DownToChangedMiles
        {
            get { return _downToChangedMiles; }
            set
            {
                if (_downToChangedMiles == value)
                    return;
                _downToChangedMiles = value;
                OnPropertyChanged("DownToChangedMiles");
            }
        }

        private string _totalMiles;
        public string TotalMiles
        {
            get { return _totalMiles; }
            set
            {
                if (_totalMiles == value)
                    return;
                _totalMiles = value;
                OnPropertyChanged("TotalMiles");
            }
        }

        private string _hkhtotalMiles;
        public string HKHTotalMiles
        {
            get { return _hkhtotalMiles; }
            set
            {
                if (_hkhtotalMiles == value)
                    return;
                _hkhtotalMiles = value;
                OnPropertyChanged("HKHTotalMiles");
            }
        }

        private bool _readOnly = true;
        public bool ReadOnly
        {
            get { return _readOnly; }
            set
            {
                if (value == _readOnly)
                    return;

                _readOnly = value;
                OnPropertyChanged("ReadOnly");
            }
        }

        private ICommand _para_EditCommand;
        public ICommand Para_EditCommand
        {
            get
            {
                if (null == _para_EditCommand)
                    _para_EditCommand = new RelayCommands(para => this.Para_EditCommand_Method(),
                        para => this.Can_Para_EditCommand_Method());

                return _para_EditCommand;
            }
        }

        private ICommand _para_Save;
        public ICommand Para_Save
        {
            get
            {
                if (null == _para_Save)
                    _para_Save = new RelayCommands(para => this.Para_Save_Method(),
                        para => this.Can_Para_Save_Method());

                return _para_Save;
            }
        }

        private ICommand _para_Cancel;
        public ICommand Para_Cancel
        {
            get
            {
                if (null == _para_Cancel)
                    _para_Cancel = new RelayCommands(para => this.Para_Cancel_Method(),
                        para => this.Can_Para_Cancel_Method());

                return _para_Cancel;
            }
        }
        #endregion

        public string Error
        {
            get { return null; }
        }

        public string this[string columnName]
        {
            get
            {
                string error = null;
                switch (columnName)
                {
                    case "InMiles":
                        error = Validate(_inMiles);
                        break;
                    case "OutMiles":
                        error = Validate(_outMiles);
                        break;
                    case "UpToChangedMiles":
                        error = Validate(_upToChangedMiles);
                        break;
                    case "HKHUpToChangedMiles":
                        error = Validate(_hkhupToChangedMiles);
                        break;
                    case "DownToChangedMiles":
                        error = Validate(_downToChangedMiles);
                        break;
                    case "TotalMiles":
                        error = Validate(_totalMiles);
                        break;
                    case "HKHTotalMiles":
                        error = Validate(_hkhtotalMiles);
                        break;
                }
                return error;
            }
        }


        public StatisticsSettingViewModel()
        {
            InitParameters();
        }

        private void InitParameters()
        {
            DataTable paraDT = _statisticsAccess.GetParaMeters();
            if (null == paraDT || paraDT.Rows.Count < 1)
            {
                MessageBoxHelper.ShowError("获取公里数、工时统计参数时出现异常！");
                return;
            }

            DicParaMeters.Add("InMiles", paraDT.Rows[0]["InMiles"].ToString());
            DicParaMeters.Add("OutMiles", paraDT.Rows[0]["OutMiles"].ToString());
            DicParaMeters.Add("UpToChangedMiles", paraDT.Rows[0]["UpToChangedMiles"].ToString());
            DicParaMeters.Add("HKHUpToChangedMiles", paraDT.Rows[0]["HKHUpToChangedMiles"].ToString());
            DicParaMeters.Add("DownToChangedMiles", paraDT.Rows[0]["DownToChangedMiles"].ToString());
            DicParaMeters.Add("TotalMiles", paraDT.Rows[0]["TotalMiles"].ToString());
            DicParaMeters.Add("HKHTotalMiles", paraDT.Rows[0]["HKHTotalMiles"].ToString());

            UpdateParas();
        }

        /// <summary>
        /// 从字典中取出参数更新
        /// </summary>
        private void UpdateParas()
        {
            DicParaMeters.TryGetValue("InMiles", out _inMiles);
            DicParaMeters.TryGetValue("OutMiles", out _outMiles);
            DicParaMeters.TryGetValue("UpToChangedMiles", out _upToChangedMiles);
            DicParaMeters.TryGetValue("HKHUpToChangedMiles", out _hkhupToChangedMiles);
            DicParaMeters.TryGetValue("DownToChangedMiles", out _downToChangedMiles);
            DicParaMeters.TryGetValue("TotalMiles", out _totalMiles);
            DicParaMeters.TryGetValue("HKHTotalMiles", out _hkhtotalMiles);

            OnPropertyChanged("InMiles");
            OnPropertyChanged("OutMiles");
            OnPropertyChanged("UpToChangedMiles");
            OnPropertyChanged("HKHUpToChangedMiles");
            OnPropertyChanged("DownToChangedMiles");
            OnPropertyChanged("TotalMiles");
            OnPropertyChanged("HKHTotalMiles");
        }

        /// <summary>
        /// 更新字典中的参数值
        /// </summary>
        private void UpdateDicParaMeters()
        {
            DicParaMeters["InMiles"] = _inMiles;
            DicParaMeters["OutMiles"] = _outMiles;
            DicParaMeters["UpToChangedMiles"] = _upToChangedMiles;
            DicParaMeters["HKHUpToChangedMiles"] = _hkhupToChangedMiles;
            DicParaMeters["DownToChangedMiles"] = _downToChangedMiles;
            DicParaMeters["TotalMiles"] = _totalMiles;
            DicParaMeters["HKHTotalMiles"] = _hkhupToChangedMiles;
        }

        private string Validate(string _value)
        {
            string error = null;
            if (string.IsNullOrEmpty(_value))
                error = "不可为空";
            if (!Regex.IsMatch(_value, @"^(\d+\.)?\d+$"))
            {
                error = "请输入正确的数字格式！";
            }
            if (_value.Length > 7)
            {
                error = "数值范围过大，请确认";
            }
            return error;
        }

        private void Para_EditCommand_Method()
        {
            this.ReadOnly = false;
        }

        private bool Can_Para_EditCommand_Method()
        {
            return _readOnly;
        }

        private void Para_Save_Method()
        {
            if (_statisticsAccess.UpdateParaMeters(_inMiles, _outMiles, _upToChangedMiles, _hkhupToChangedMiles, _downToChangedMiles, _totalMiles, _hkhtotalMiles))
            {
                MessageBoxHelper.ShowTips("统计参数修改保存成功！");
                UpdateDicParaMeters();

                ReadOnly = true;
            }
            else
            {
                MessageBoxHelper.ShowError("保存失败，请确认格式输入正确后重试！");
                UpdateParas();  //失败便返回原来的参数
            }
        }

        private bool Can_Para_Save_Method()
        {
            return !_readOnly && string.IsNullOrEmpty(Validate(_inMiles))
                && string.IsNullOrEmpty(Validate(_outMiles))
                && string.IsNullOrEmpty(Validate(_upToChangedMiles))
                && string.IsNullOrEmpty(Validate(_hkhupToChangedMiles))
                && string.IsNullOrEmpty(Validate(_downToChangedMiles))
                && string.IsNullOrEmpty(Validate(_totalMiles))
                && string.IsNullOrEmpty(Validate(_hkhtotalMiles));
        }

        private void Para_Cancel_Method()
        {
            UpdateParas();
            ReadOnly = true;
        }

        private bool Can_Para_Cancel_Method()
        {
            return !_readOnly;
        }

    }
}
